package com.wandian.texas.command.texas;

import java.util.ArrayList;

import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.CommandObject;
import com.wandian.texas.RoomMgr;
import com.wandian.texas.SessionObject;
import com.wandian.texas.UserMgr;
import com.wandian.command.Cmd;
import com.wandian.command.UserCommand;
import com.wandian.texas.entity.UserInfoEntity;
import com.wandian.texas.protocol.Protocol;

@Cmd(code = Protocol.RoomUserListCmd, desc = "大厅,显示房间内玩家列表信息")
public class RoomUserListCmd extends UserCommand {

	@Override
	public void execute(BaseCommandObject cmdObject) {
		super.execute(cmdObject);
		byte roomid = cmdObject.readByte();
		sendRoomUserList(cmdObject, roomid);
	}

	/**
	 * 将房间内玩家列表信息发送过去
	 * 
	 * @param cmdObject
	 */
	private void sendRoomUserList(BaseCommandObject cmdObject, int roomid) {
		ArrayList<SessionObject> roomusers = RoomMgr.getRoom(roomid).roomUsers();
		int userCount = 0;
		cmdObject.writeBegin();
		//		 int userLengthPos = cmdObject.position();
		if (roomusers != null && roomusers.size() > 0) {
			userCount = roomusers.size();
		}
		cmdObject.writeInt(userCount);
		UserInfoEntity user;
		for (int i = 0; i < userCount; i++) {
			user = roomusers.get(i).getUser();
			cmdObject.writeInt(user.getUserId());
			cmdObject.writeString(user.getIcon());
			cmdObject.writeString(user.getNickName());
			cmdObject.writeByte(user.getSex());
//			cmdObject.writeInt(user.getGold());
			cmdObject.writeFloat(user.getMoney());
			cmdObject.writeByte(user.getStatus());
			cmdObject.writeInt(user.getRank());
		}
		cmdObject.writeEnd();
		UserMgr.I().SendData(cmdObject);
	}
}
